﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/2/9 23:25:58
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;
using System.Data.SqlClient;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 单位数据访问对象
    /// </summary>
    public class UnitDAO : IUnitDAO
    {
        #region Public functions
        public int Create(RS_Unit unit)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    INSERT INTO RS_Unit
                            (UnitName)
                        VALUES
                            (@UnitName)
                    SELECT @@IDENTITY";

                return Convert.ToInt32(opt.ExecuteScalar(
                    sql,
                    DBUtil.CreateSqlParameter(unit.UnitName, "UnitName")));
            }
        }

        public List<RS_Unit> ReadAll()
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_Unit> units = new List<RS_Unit>();
                string sql = @"
                    SELECT *
                        FROM RS_Unit";

                SqlDataReader reader = opt.ExecuteQuery(sql);

                while (reader.Read())
                {
                    object unit = new RS_Unit();

                    DBUtil.ReadData(reader, ref unit);

                    units.Add(unit as RS_Unit);
                }

                return units;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        public DBOptResultType Delete(int unitId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_Unit        " +
                    "   WHERE UnitId = @UnitId  ";

                return (DBOptResultType)opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(unitId, "UnitId"));
            }
        }
        #endregion
    }
}
